package com.neusoft.elmboot.service.impl;

import com.neusoft.elmboot.mapper.BusinessMapper;  // 引入 BusinessMapper
import com.neusoft.elmboot.po.Business;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.elmboot.mapper.MerchantMapper;
import com.neusoft.elmboot.po.Merchant;
import com.neusoft.elmboot.service.MerchantService;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class MerchantServiceImpl implements MerchantService {

	@Autowired
	private MerchantMapper merchantMapper;  // 保留 MerchantMapper

	@Autowired
	private BusinessMapper businessMapper;  // 新增 BusinessMapper

	@Override
	public Merchant checkMerchantStatus(String userId) {
		return merchantMapper.checkMerchantStatus(userId);
	}

	@Override
	public int saveBusiness(Business business) {
		// 插入 business 表，获取自增的 businessId
		int result = businessMapper.insertBusiness(business);

		// 如果 business 表插入成功，则继续插入 merchant 表
		if (result > 0) {
			Merchant merchant = new Merchant();
			merchant.setUserId(business.getUserId());
			merchant.setBusinessId(business.getBusinessId());  // 使用自动生成的 businessId
			merchant.setBusinessName(business.getBusinessName());  // 设置商家名称
			merchant.setBusinessAddress(business.getBusinessAddress());  // 设置商家地址
			merchant.setIsActive(true);  // 设置商家状态为激活
			merchantMapper.insertMerchant(merchant);  // 插入 merchant 表
		}

		return result;
	}
}
